﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SaladBar.Service.Repositories.Utilities
{
    public class WhereBuilder<T>
    {
        private List<Func<T, bool>> _lambdas = new List<Func<T, bool>>();

        public WhereBuilder<T> Where(Func<T, bool> where)
        {
            this._lambdas.Add(where);
            return this;
        }

        public Func<T, bool> Build()
        {
            Func<T, bool> where = null;
            if (this._lambdas.Count() == 0)
            {
                return t => true;
            }
            else
            {
                foreach (var lambda in this._lambdas)
                {
                    if (where == null)
                        where = lambda;
                    else
                        where = (o) => where(o) && lambda(o);
                }
            }
            return where;
        }
    }
}
